package com.lsp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.lsp.base.empty.EmptyLayout;
import com.lsp.base.empty.EmptyLayoutDelegate;
import com.lsp.base.empty.EmptyLayoutDelegateImpl;

/**
 * 空页面基类
 *
 * @author com.lsp.ibxs
 * @created 16/11/28 14:08
 */
public abstract class BaseEmptyFragment extends BaseFragment implements EmptyLayoutDelegate {

  private EmptyLayout emptyLayout;
  private EmptyLayoutDelegateImpl emptyLayoutDelegateImpl;

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    emptyLayoutDelegateImpl = new EmptyLayoutDelegateImpl();
    return super.onCreateView(inflater, container, savedInstanceState);
  }

  @Override protected void initEmptyLayout() {
    super.initEmptyLayout();
    if (getEmptyId() > 0) {
      emptyLayout = (EmptyLayout) mLayoutInflater.inflate(getEmptyId(), null);
      llContentContainer.addView(emptyLayout, LinearLayout.LayoutParams.MATCH_PARENT,
          LinearLayout.LayoutParams.MATCH_PARENT);
    }
  }

  @Override public void setLoadDataSucced(EmptyLayout emptyLayoutOut) {
    emptyLayoutDelegateImpl.setLoadDataSucced(
        emptyLayoutOut != null ? emptyLayoutOut : emptyLayout);
  }

  @Override
  public void setLoadDataError(EmptyLayout emptyLayoutOut, View.OnClickListener mOnClickListener) {
    if (emptyLayoutOut == null && emptyLayout == null) {
      return;
    }
    emptyLayoutDelegateImpl.setLoadDataError(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout,
        mOnClickListener);
  }

  @Override
  public void setNoData(EmptyLayout emptyLayoutOut, View.OnClickListener mOnClickListener) {
    emptyLayoutDelegateImpl.setNoData(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout,
        mOnClickListener);
  }

  @Override public void setLoading(EmptyLayout emptyLayoutOut) {
    emptyLayoutDelegateImpl.setLoading(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout);
  }
}
